from banner_tools.event_utils import OrderedEventBase, OrderedEventList, List
from bdtime import tt, Time


class ProcessingBase:
    def __init__(self, room_id=0):
        self.room_id = room_id
        self.on_enter_event_list = OrderedEventList(room_id)
        self.on_exit_event_list = OrderedEventList(room_id)

    def on_enter_call(self):
        """进入时"""
        print('--- on_enter_call')
        self.on_enter_event_list.run_all_events()

    def on_running_call(self):
        print('--- on_running_call')

    def on_exit_call(self):
        """离开时"""
        print('--- on_enter_call')
        self.on_exit_event_list.run_all_events()


def test_processing(pb=None):
    if pb is None:
        pb = ProcessingBase()

    room_id = pb.room_id

    class TestEvent(OrderedEventBase):
        def run(self):
            timer = Time()
            while timer.during(1):
                print(f'------ on TestEvent.run --- now: {timer.now()} --- {self}')
                timer.sleep(0.5)

    e1 = TestEvent(room_id, '开始事件')
    e2 = TestEvent(room_id, '结束事件')

    pb.on_enter_event_list.add_event(e1)

    pb.on_exit_event_list.add_event(e2)

    tt.sleep(1)

    pb.on_enter_call()
    print('=========== On ProcessingBase')

    pb.on_running_call()

    pb.on_exit_call()

    # assert False, "错误测试!"


def test():
    test_processing()


if __name__ == '__main__':
    test()

